package com.java.tcpsocket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientThread extends Thread {

	private Socket socket;

	@Override
	public void run() {

		try {
			// 初始化
			socket = new Socket("10.96.102.94", 8811);
			/**
			 * 客户端发送消息
			 */
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "utf-8"));
			String str = "你好！我是客户端！";
			bw.write(str);
			System.out.println("客户端发送消息：" + str);
			// 换行
			bw.newLine();
			// 清空
			bw.flush();
			/**
			 * 客户端接收消息
			 */
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "utf-8"));
			String str1 = br.readLine();
			System.out.println("客户端接收消息：" + str1);

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
